/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsf.view;

import com.jsf.entity.User;
import com.jsf.rest.UserFacadeREST;
import com.jsf.util.CommonUtil;
import java.util.Date;
import java.util.List;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author gang
 */
@Named
public class UserView extends PageViewBean{
    
    private List<User> userList;
    
    private String test;
    
    private User user;
    
    private List<User> filterUser;
    
    @Inject
    private UserFacadeREST userRest;

    @Override
    protected void init() {
        log.debug("init success");
        newInit();
        newMethod();
    }

    private void newInit() {
        user = new User();
        test="111";
    }

    private void newMethod() {
        findAll();
        int i=0;
        for(User user:userList){
            log.debug(user.getUsername());
            log.debug("执行次数"+(i++));
        }
        
    }
    
    public void findAll(){
        setUserList(getUserRest().findAll());
    }
    
    public void create(){
        log.debug("create"+user);
        if(createHelp(user)){
            userRest.create(user);
            info("添加"+user.getUsername()+"成功");
            updateList();
        }else{
            info("添加"+user.getUsername()+"失败");
        }
    }
    
    public boolean createHelp(User user){
        user.setCreatetime(new Date());
        return true;
    } 
    
    public void updateList(){
        findAll();
    }
    
    /**
     * @return the test
     */
    public String getTest() {
        return test;
    }

    /**
     * @param test the test to set
     */
    public void setTest(String test) {
        this.test = test;
    }

    /**
     * @return the userList
     */
    public List<User> getUserList() {
        return userList;
    }

    /**
     * @param userList the userList to set
     */
    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    /**
     * @return the userRest
     */
    public UserFacadeREST getUserRest() {
        return userRest;
    }

    /**
     * @param userRest the userRest to set
     */
    public void setUserRest(UserFacadeREST userRest) {
        this.userRest = userRest;
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * @return the filterUser
     */
    public List<User> getFilterUser() {
        return filterUser;
    }

    /**
     * @param filterUser the filterUser to set
     */
    public void setFilterUser(List<User> filterUser) {
        this.filterUser = filterUser;
    }
    
}
